/**
 * @title: DynaSqlProvider.java
 * @package com.wehealth.weiyu.common.data.mybatis
 * @description: TODO
 * @author hushsh
 * @date 2016年12月30日下午4:59:00
 * @version V1.0
 */

package com.apiserver.data.mybatis.sqlProvider;


import com.apiserver.data.mybatis.model.EntityTable;
import com.apiserver.data.mybatis.util.EntityUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.jdbc.SQL;

import java.util.List;
import java.util.Map;

/**
 * @Author feizhou
 * @Description 生成mybatis动态sql
 * @Date 下午 2:33 2019/10/13 0013
 * @Param
 * @return
 **/

@Slf4j
public class DelProvider extends Provider {


    /**
     * @return java.lang.String
     * @Author feizhou
     * @Description 删除语句，根据传进来的有值参数进行删除
     * @Date 下午 2:36 2019/10/13 0013
     * @Param [m]
     **/

    public <M> String delete(Map<String, Object> param) {
        M m = (M) param.get("m");

        EntityTable entity = EntityUtil.getEntityTable(m);
        SQL sql = new SQL()
                .DELETE_FROM(entity.getTableName())
                .WHERE("1=1");


        sql = getWhere(m, sql, entity);

        log.debug(">>>>sql:   ", sql.toString());

        System.out.println(">>>>sql:   " + sql.toString());
        return sql.toString();
    }

    /**
     * @return java.lang.String
     * @Author feizhou
     * @Description 删除语句，根据传进来的有值参数进行删除
     * @Date 下午 2:36 2019/10/13 0013
     * @Param [m]
     **/

    public <M> String deleteById(Map<String, Object> param) {

        Class entityClass = (Class) param.get("entityClass");


        EntityTable entity = EntityUtil.getEntityTable(entityClass);


        String PKColumn = entity.getPKColumn();
        String PKProperty = entity.getPKProperty();

        SQL sql = new SQL()
                .DELETE_FROM(entity.getTableName())
                .WHERE("1=1");

        sql = sql.WHERE(PKColumn + "=#{" + PKProperty + "}");

        log.debug(">>>>sql:   ", sql.toString());
        System.out.println(">>>>sql:   " + sql.toString());
        return sql.toString();
    }

    /**
     * @return java.lang.String
     * @Author feizhou
     * @Description 删除语句，根据传进来的有值参数进行删除
     * @Date 下午 2:36 2019/10/13 0013
     * @Param [m]
     **/

    public <M> String deleteByIds(Map<String, Object> param) {

        Class entityClass = (Class) param.get("entityClass");
        List<Object> ids = (List<Object>) param.get("ids");

        EntityTable entity = EntityUtil.getEntityTable(entityClass);


        String PKColumn = entity.getPKColumn();

        SQL sql = new SQL()
                .DELETE_FROM(entity.getTableName())
                .WHERE("1=1");


//		sql = sql.WHERE(PKColumn + " in (" + ListToSelectForIn( ids) + ")");

        sql = getWhereForIn(ids, sql, entity, PKColumn);


        log.debug(">>>>sql:   ", sql.toString());
        System.out.println(">>>>sql:   " + sql.toString());
        return sql.toString();
    }


}
